package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Cx
 * @stage JT Of Fourth stage
 * @creat 2020-11-26 10:05
 */
@Controller
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("doFindAll")
    public String doFindAll(Model model) {
        List<User> userList = userService.findAll();
        model.addAttribute("userList", userList);
        return "userList";
    }

    /**
     * 跳转页面 ajax.jsp
     */
    @RequestMapping("toAjax")
    public String toAjax() {
        return "ajax";
    }

    /**
     * 接收ajax请求: /doFindAjax
     */
    @RequestMapping("doFindAjax")
    @ResponseBody /* 将返回值结果转换为JSON格式 代表ajax结束 */
    public List<User> doFindAjax() {
        return userService.findAll();
    }

}
